var billing = {
   mandatoryFiledsElement: ['#studentName', '#standard', '#totalRs'],
   mandatoryFileds: ['please enter the student name.', 'please choose the standard.', 'There is no Rs total entry, to generate bill.'],
   savePrintBill: function() { 
     var errorMessage = billing.checkMandatoryElementsField();
     
	if (errorMessage != '') { 
	  alert(errorMessage);
	} else {  
	    $('form#billFormContainer').attr('action', BASE_URL + 'index.php/billing/savePrintBill');
	    $('form#billFormContainer').submit();
	}
   },
   saveBill: function() { 
     var errorMessage = billing.checkMandatoryElementsField();
     
	if (errorMessage != '') { 
	  alert(errorMessage);
	} else {  
	    $('form#billFormContainer').attr('action', BASE_URL + 'index.php/billing/saveBill');
	    $('form#billFormContainer').submit();
	}
   },
   calculateTotal: function() {
     var total = 0;
    $('input.feeEntryInputTextRs').each(function() { 
	  total += Number($(this).val()); 
    });
    $('input.feeEntryInputTextP').each(function() { 
	  total += (Number($(this).val()) / 100); 
    });
    return total;
  },
  checkMandatoryElementsField: function() {
    var errorMessage = '';
    for (var mandatoryFiledsIndex in billing.mandatoryFiledsElement) { 
       if ($(billing.mandatoryFiledsElement[mandatoryFiledsIndex]).val() === '' || $(billing.mandatoryFiledsElement[mandatoryFiledsIndex]).val() === '--Select--') { 
	 errorMessage += '\n' + billing.mandatoryFileds[mandatoryFiledsIndex];
      }
    }
    return errorMessage;
  }
};

$(document).ready(function() { 
      $('input#savePrintBill').click(function() { 
	    billing.savePrintBill();
      });
      $('input#saveBill').click(function() { 
	    billing.saveBill();
      });
      $('input.feeEntryInputTextRs, input.feeEntryInputTextP').keyup(function() { 
	  if (billing.calculateTotal() == 0) {
	    $('input#totalRs').val('');
	  } else { 
	    $('input#totalRs').val(billing.calculateTotal());
	  }
      });
      $('#pagination a').click(function(e){
	  e.preventDefault();
	 // alert("ff");
	  var str = $(this).attr('href');
	  var offset = (str.split("page=")[1] != '') ? (parseInt(str.split("page=")[1])) : '0';
	  $('.current_page').val(offset);
	  $('#search_button').click();
      });
});